package com.util.common.page;

import org.springframework.stereotype.Repository;

@Repository("pageImp")
public class PageImp implements Page {
	private int totalRows = 0;//
	private int pageSize = 10;//
	private int currentPage = 1;//
	private int totalPages = 1;
	private int startRow = 0;
	private String pageMethod = "";
	
	/**
	 * 获得当前页
	 * @return 返回一个整型
	 */
	public int getCurrentPage(){
		return this.currentPage;
	}
	
	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	@Override
	public void setPageSize(String newPageSize) {
		pageSize = Integer.parseInt(newPageSize);
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	/**
	 * 获得当每页显示的记录数
	 * @return 返回一个整型
	 */
	public int getPageSize(){
		return pageSize;
	}
	
	public void setPageMethod(String pageMethod){
		this.pageMethod = pageMethod;
	}
	
	public String getPageMethod(){
		return this.pageMethod;
	}
	
	public int getStartRow(){
		return this.startRow;
	}
	
	//
	public void first(){
		currentPage = 1;
		this.startRow = 0;
	}
	//
	public void previous(){
		if(currentPage <= 1){
			currentPage = 1;
			return;
		}
		currentPage--;
		startRow = (currentPage - 1)*pageSize;
	}
	//
	public void next(){
		if(currentPage < totalPages){
			currentPage++;
			startRow = (currentPage-1)*pageSize;
		}else if(currentPage == totalPages){
			startRow = (currentPage-1)*pageSize;
		}else if(currentPage < 1){
			currentPage = 1;
			startRow = 0;
		}
	}
	//
	public void last(){
		currentPage = totalPages;
		startRow = (currentPage - 1)*pageSize;
	}
	
	public void pageGoto(){
		startRow = (currentPage-1)*pageSize;
	}
	
	public void refresh(int _currentPage){
		this.currentPage = _currentPage;
		if(this.currentPage > this.totalPages || this.startRow > this.totalRows){
			this.currentPage = this.totalPages;
			this.startRow = (currentPage - 1)*pageSize;
		}
		if(this.currentPage < 1 || this.startRow < 0){
			this.currentPage = 1;
			this.startRow = 0;
		}
	}
	
	public void pagePeel(){
		if(this.pageMethod != null){
			if("first".equals(this.pageMethod)){
				this.first();
			}else if("previous".equals(this.pageMethod)){
				this.previous();
			}else if("next".equals(pageMethod)){
				this.next();
			}else if("last".equals(pageMethod)){
				this.last();
			}else if("goto".equals(pageMethod)){
				this.pageGoto();
				this.refresh(this.currentPage);//刷新页面号，防止超出边界
			}else if(this.pageMethod.length()>2 && "Rows".equals(this.pageMethod.substring(2))){
				pageSize = Integer.parseInt(this.pageMethod.substring(0,2));
				//当页面显示行数变动时需要重新计算总页数
				int totalPages = totalRows/this.getPageSize();
				if(totalRows%this.getPageSize() > 0){
					totalPages += 1;
				}
				this.setTotalPages(totalPages);//设置总页数
				this.setPageMethod("");//防止页面显示行数不变时重复计算总页数和总行数
			}
		}
		this.refresh(this.currentPage);
		System.out.println("开始行："+this.startRow+"当前页："+this.currentPage);
	}
	
	@Override
	public void pagePeel(String pageNumber) {
		
	}

}
